package m2.component;
import java.util.ArrayList;

import m2.port.Port;
import m2.service.Service;

public abstract class ComponentInterface{

        private ArrayList<Service> listeService;
        private ArrayList<Port> ports;
       
        /**
         * 
         */
        public ComponentInterface(){
                listeService = new ArrayList<Service>();
                ports = new ArrayList<Port>();
        }
       
        /**
         * 
         * @param nameService
         * @return
         */
        public final Service getService(String nameService) {
                for (Service service : listeService){
                        if (nameService.equals(service.getNameService())){
                                return service;
                        }
                }
                return null;
        }
       
        /**
         * 
         * @param namePort
         * @return
         */
        public final Port getPort(String namePort) {
                for (Port port : ports){
                        if (namePort.equals(port.getClass().getSimpleName())){
                                return port;
                        }
                }
                return null;
        }
       
        /**
         * 
         * @param nameService
         * @param namePorts
         */
        public void serviceUsePorts(String nameService, String ... namePorts){        		
                Service service = getService(nameService);
                Port[] ports = new Port[namePorts.length];
               
                for (int i = 0; i < namePorts.length; i ++){
                        ports[i] = getPort(namePorts[i]);
                }
                service.usedPort(ports);
        }
        
        /**
         * 
         * @param port
         */
        public final void setPort(Port port) {
                this.ports.add(port);
        }
       
        /**
         * 
         * @return
         */
        public final ArrayList<Service> getListeService() {
                return listeService;
        }

        /**
         * 
         * @param service
         */
        public final void setService(Service service) {
                listeService.add(service);
        }
        

        /**
         * 
         * @param serviceName
         * @return
         */
        public Port getPortOfService(String serviceName){
    		for(Service service : listeService){
    			if (service.getNameService().equals(serviceName)){
    				for (Port port : service.getListePort()){
    					return port;
    				}        				
    			}
    		}
        	return null;
        }
        
        public abstract void buildService();
}
